package org.skipper.samples.excel;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author shoujian
 * Date: 2022-10-17
 * Time: 16:31
 */
public class CheckUtil {

    public static void main(String[] args) {
        List<Map<String, Object>> dataList = PoiUtil.read("/Users/shoujian/Documents/00work/临时文件/query-impala-36403.xlsx");

        String tagStr = "[{\"tagId\":\"etqN3RDAAAha8QPuF41we5Qz4zyaU-Sg\",\"cityName\":\"北京市\",\"cityId\":1},{\"tagId\":\"etqN3RDAAAvW9UMvP0DIhQXfuE6Vxv2Q\",\"cityName\":\"上海市\",\"cityId\":2},{\"tagId\":\"etqN3RDAAAEWKVlA_AtmrTtqunn1pBbw\",\"cityName\":\"重庆市\",\"cityId\":3},{\"tagId\":\"etqN3RDAAAQJZU6BvwdAsuTpdbiKQcEQ\",\"cityName\":\"安徽省\",\"cityId\":4},{\"tagId\":\"etqN3RDAAApKB68wDOZPdp_ThsmTnfPA\",\"cityName\":\"福建省\",\"cityId\":5},{\"tagId\":\"etqN3RDAAAfEbULANon7fJuTMCd8v4Ag\",\"cityName\":\"甘肃省\",\"cityId\":6},{\"tagId\":\"etqN3RDAAAq5oJKPmcsA4Y4fMpDQiFdQ\",\"cityName\":\"广东省\",\"cityId\":7},{\"tagId\":\"etqN3RDAAA0g4syROy_orR9VEpUEytmg\",\"cityName\":\"广西壮族自治区\",\"cityId\":8},{\"tagId\":\"etqN3RDAAA-RAyT-02eRieQ0N39RH1Kg\",\"cityName\":\"贵州省\",\"cityId\":9},{\"tagId\":\"etqN3RDAAACawpJXfO8GKRBXRGcvzY_g\",\"cityName\":\"海南省\",\"cityId\":10},{\"tagId\":\"etqN3RDAAADu-C7AXN16aCJjkE8phB4w\",\"cityName\":\"河北省\",\"cityId\":11},{\"tagId\":\"etqN3RDAAAIgssPpJOXiYz8wxVjPqRsQ\",\"cityName\":\"黑龙江省\",\"cityId\":12},{\"tagId\":\"etqN3RDAAAB2ZI4g_jTmR3JGPUAeCdKA\",\"cityName\":\"河南省\",\"cityId\":13},{\"tagId\":\"etqN3RDAAAhZ9JjIsPSavNEOZlDaq71Q\",\"cityName\":\"香港特别行政区\",\"cityId\":14},{\"tagId\":\"etqN3RDAAAVCXtc9Fv6PXqDuAmbEVBIQ\",\"cityName\":\"湖北省\",\"cityId\":15},{\"tagId\":\"etqN3RDAAADLSZrZPKNATSTOKxoWQUIg\",\"cityName\":\"湖南省\",\"cityId\":16},{\"tagId\":\"etqN3RDAAAd84uMvtvrg9-o2uCoCxDUQ\",\"cityName\":\"江苏省\",\"cityId\":17},{\"tagId\":\"etqN3RDAAAGlkm9v3X-tkuQlVkjZY3ug\",\"cityName\":\"江西省\",\"cityId\":18},{\"tagId\":\"etqN3RDAAA2OJQ-0Y59GTud1y_AGQcDg\",\"cityName\":\"吉林省\",\"cityId\":19},{\"tagId\":\"etqN3RDAAAJ8MLqXbotx-cTqWAPNWuLQ\",\"cityName\":\"辽宁省\",\"cityId\":20},{\"tagId\":\"etqN3RDAAAi0H1aG5vSqVe90EZfoCuOg\",\"cityName\":\"澳门特别行政区\",\"cityId\":21},{\"tagId\":\"etqN3RDAAAV-TUrfSAGcenAVkrxDkx7w\",\"cityName\":\"内蒙古自治区\",\"cityId\":22},{\"tagId\":\"etqN3RDAAAxzeBIbsfxX2rq_5Xbbntig\",\"cityName\":\"宁夏回族自治区\",\"cityId\":23},{\"tagId\":\"etqN3RDAAAlRgGYYegLHXBk0Dza1fZaA\",\"cityName\":\"青海省\",\"cityId\":24},{\"tagId\":\"etqN3RDAAATKtQOfMAoPuAXY_3S6BsoQ\",\"cityName\":\"山东省\",\"cityId\":25},{\"tagId\":\"etqN3RDAAAg6GrDuV2NTLkEmsLcvj8ig\",\"cityName\":\"山西省\",\"cityId\":26},{\"tagId\":\"etqN3RDAAALqtwUQG5i6QDW1sWYMPFFw\",\"cityName\":\"陕西省\",\"cityId\":27},{\"tagId\":\"etqN3RDAAAdHu8MVgFi17nQnetspCg2g\",\"cityName\":\"四川省\",\"cityId\":28},{\"tagId\":\"etqN3RDAAAp_YLCF69OD9GcqqZqIiJsA\",\"cityName\":\"台湾省\",\"cityId\":29},{\"tagId\":\"etqN3RDAAA5wTo1-ShHRKrjeITm2ih8A\",\"cityName\":\"天津市\",\"cityId\":30},{\"tagId\":\"etqN3RDAAA3NY0nQnW-7eaT6ddgJjMJQ\",\"cityName\":\"新疆维吾尔自治区\",\"cityId\":31},{\"tagId\":\"etqN3RDAAANW7bq4Zr16AMSzmKWUMz7g\",\"cityName\":\"西藏自治区\",\"cityId\":32},{\"tagId\":\"etqN3RDAAAyl76opoL0y6HFiBQStYAFw\",\"cityName\":\"云南省\",\"cityId\":33},{\"tagId\":\"etqN3RDAAAI0fV4K_oiIslEspfFZmOfA\",\"cityName\":\"浙江省\",\"cityId\":34},{\"tagId\":\"etqN3RDAAADrQdO_GI89MVN5scv3d0bA\",\"cityName\":\"国外\",\"cityId\":35}]";

        List<CityTag> cityTagList = JSON.parseArray(tagStr, CityTag.class);

        Map<Integer, CityTag> cityTagMap = cityTagList.stream().collect(Collectors.toMap(CityTag::getCityId, a -> a));

        for (Map<String, Object> stringObjectMap : dataList) {

            try {
                String token = "8y5DsFHCCc53XIrDpqjqnSOTvpHdXC5aefSroE-VIB7S4PZIMFe2VcH34v-ixAiDRS86qC5Jp93Ex0Q0Bhz08mAZFW5tZYNPyUrdloOFeHXV3Gha94A37v2fBf7oxvcRiOy1wkvZyuiwgFb99s6ZonddO4pFSSXa9OP9tlcKAp6O2bKtykgRmZ1rRu65NDrYuPuNOgrrjGKC7x-KH-NgHg";
                String url = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get";
                String s = PoiUtil.httpSendMessage(url, "?access_token=" + token + "&external_userid=" + (String) stringObjectMap.get("external_user_id"));
                JSONObject jsonObject = JSON.parseObject(s);

                String cityName = (String) stringObjectMap.get("name_i");

                if (cityName == null || cityName.equals("NULL")) {
                    cityName = (String) stringObjectMap.get("name");
                }

                if (cityName == null || !s.contains(cityName)) {
//                    System.out.println("error:" + stringObjectMap.toString());
//                    System.out.println(jsonObject);
                    Integer cityId = Integer.parseInt((String) stringObjectMap.get("parent_id"));
                    if (cityId == 0) {
                        cityId = Integer.parseInt((String) stringObjectMap.get("city_id_i"));
                    }

                    if (cityId == null || cityTagMap.get(cityId) == null) {
                        continue;
                    }

                    String userId = (String) stringObjectMap.get("company_user_id");
                    if (jsonObject.get("follow_user") != null && ((JSONArray) jsonObject.get("follow_user")).getJSONObject(0) != null &&
                            ((JSONArray) jsonObject.get("follow_user")).getJSONObject(0).get("userid") != null) {
                        userId = (String) ((JSONArray) jsonObject.get("follow_user")).getJSONObject(0).get("userid");
                    }


                    try {
                        AddExternalUserMarkDTO addExternalUserMarkDTO = new AddExternalUserMarkDTO();
                        addExternalUserMarkDTO.setUserId(userId);
                        addExternalUserMarkDTO.setExternalUserid((String) stringObjectMap.get("external_user_id"));
                        addExternalUserMarkDTO.setAddTag(Collections.singletonList(cityTagMap.get(cityId).getTagId()));
                        String params = JSON.toJSONString(addExternalUserMarkDTO);
                        String tagUrl = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/mark_tag?access_token=" + token;
                        String tagResult = PoiUtil.doPostBody(tagUrl, params);
                        JSONObject jsonObjectTag = JSON.parseObject(tagResult);

                        Integer errcode = jsonObjectTag.getInteger("errcode");

                        if (errcode != 0) {
                            System.out.println("tagError:" + stringObjectMap.toString());
                            System.out.println(jsonObjectTag);
//                            break;
                        }
                        System.out.println("tagSuccess:" + stringObjectMap.toString());


//                    Thread.sleep(100);

                    } catch (Exception e) {
                        System.out.println("exception:" + stringObjectMap.toString());
//                        break;
                    }
//                    break;
                }
                System.out.println("success:" + stringObjectMap.get("id"));

            } catch (Exception e) {
                System.out.println("exception:" + stringObjectMap.toString());
//                break;
            }

        }


    }

}
